import { ResConfig } from "../../SysConfig/SysConfig";
import ResManage from "../ResManage";
import UIManage from "./UIManage";



const { ccclass, property } = cc._decorator;

@ccclass
export default class UIBasic extends cc.Component {
    /**图集引用计数记录 */
    private atlasRefMap: Map<string, number> = new Map();
    /**图片引用计数记录 */
    private imageRefMap: Map<string, number> = new Map();

    /**UI面板唯一标识，非手动赋值 */
    public ui_uid: number = -1;
    /**UI面板关闭时间记录，非手动赋值 */
    public ui_time: number = 0;
    /**UI面板状态记录，非手动赋值 <0代表初始状态，1代表关闭中状态>*/
    public ui_state: number = 0;


    onLoad() {

    }

    start() {

    }

    /**
     * 调用该函数加载图片，图片内存资源自动进行管理。
     * @param imageUrl  图片路径
     * @param sprite    Sprite组件
     * @param callback  回调函数默认null
     */
    protected loadImag(imageUrl: string, sprite: cc.Sprite, callback: Function = null) {
        ResManage.Instance.loadAssetByBoundle(ResConfig.ImageBundle, imageUrl, cc.SpriteFrame, (res: cc.SpriteFrame) => {
            if (res) {
                sprite.spriteFrame = res;
                if (callback) {
                    callback();
                }

                if (this.imageRefMap.has(imageUrl)) {
                    let count = this.imageRefMap.get(imageUrl);
                    this.imageRefMap.set(imageUrl, ++count);
                }
                else {
                    this.imageRefMap.set(imageUrl, 1);
                }
            }
        });
    }

    /**
     * 调用该函数加载图集，图集内存资源自动进行管理。
     * @param atlasUrl  图集路径
     * @param sprite    Sprite组件
     * @param imageUrl  图片名号
     * @param callback  回调函数默认null
     */
    protected loadImagByAtlas(atlasUrl: string, sprite: cc.Sprite, imageUrl: string, callback: Function = null) {
        ResManage.Instance.loadAssetByBoundle(ResConfig.ImageBundle, atlasUrl, cc.SpriteAtlas, (res: cc.SpriteAtlas) => {
            if (res) {
                sprite.spriteFrame = res.getSpriteFrame(imageUrl);
                if (callback) {
                    callback();
                }

                if (this.atlasRefMap.has(atlasUrl)) {
                    let count = this.atlasRefMap.get(atlasUrl);
                    this.atlasRefMap.set(atlasUrl, ++count);
                }
                else {
                    this.atlasRefMap.set(atlasUrl, 1);
                }
            }
        });
    }



    protected onDestroy(): void {
        //释放内存图片
        if (this.imageRefMap.size != 0) {
            this.imageRefMap.forEach((value, key) => {
                for (let i = 0; i < value; i++) {
                    ResManage.Instance.releaseAssetByBoundle(ResConfig.ImageBundle, key, cc.SpriteFrame);
                }
            })
        }

        //释放内存图集
        if (this.atlasRefMap.size != 0) {
            this.atlasRefMap.forEach((value, key) => {
                for (let i = 0; i < value; i++) {
                    ResManage.Instance.releaseAssetByBoundle(ResConfig.ImageBundle, key, cc.SpriteAtlas);
                }
            })
        }


        this.atlasRefMap.clear();
        this.imageRefMap.clear();

        //释放内存预制体面板
        if (this.ui_uid != -1) {
            UIManage.Instance.InstantReleaseUIPannel(this.ui_uid);
            this.ui_uid = -1;
        }
    }

}
